<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\common\model;

use app\common\model\Base;
use think\Db;
use traits\model\SoftDelete;

class Thread extends Base {

    use SoftDelete;

    protected $deleteTime = 'delete_time';

    public function getRecommendTextAttr($value, $data) {
        return $data['recommend'] == 1 ? '推荐' : '';
    }

    public function getTopTextAttr($value, $data) {
        return $data['top'] == 1 ? '置顶' : '';
    }

    public function thread_edit($post) {

        $id = $post['id'] ?? 0;

        $data['cid'] = $post['cid'] ?? '';
        $data['title'] = $post['title'] ?? '';
        $data['content'] = $post['content'] ?? '';
        $data['points'] = $post['points'] ?? '';
        $data['update_time'] = time();
        $data['member_id'] = $post['member_id'];

        if ($id) {

            return db('thread')->where('id', $id)->update($data);
        } else {

            return 'ID缺失';
        }
    }

    public function thread_add($post, $member_id) {


        $data['cid'] = $post['cid'] ?? '';
        $data['title'] = $post['title'] ?? '';
        $data['content'] = $post['content'] ?? '';
        $data['points'] = $post['points'] ?? '';
        $data['status'] = 0;
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['member_id'] = $member_id;

        //有些用户真烦人，非要给你数据库注入点垃圾     
        foreach ($data as $key => $value) {
            $data[$key] = strip_tags($value);
        }


        // 
        $insert_id = db('thread')->insertGetId($data);

        return $insert_id;
    }

    public function model_where($wheres = []) {

        foreach ($wheres as $key => $value) {
            $this->where($value[0], $value[1], $value[2]);
        }


        if (request()->get('timea'))
            $this->where('a.create_time', '>=', strtotime(request()->get('timea') . ' 00:00:00'));
        if (request()->get('timeb'))
            $this->where('a.create_time', '<=', strtotime(request()->get('timeb') . ' 23:59:59'));


        if (request()->get('keyword'))
            $this->where('title', 'like', '%' . request()->get('keyword') . '%');


        $this->join('member m', 'm.id = a.member_id', 'LEFT');
        $this->join('member_ident mi', 'mi.member_id = m.id', 'LEFT');
        $this->join('thread_column tc', 'tc.id = a.cid', 'LEFT');
        // $this->join('member m2', 'm2.id = tc.member_id', 'LEFT');

        $this->alias('a');

        $this->order('a.top desc,a.id desc');

        $this->field('a.*,m.id as member_id,m.nickname,m.avatar,m.sex,m.vip,m.ident,mi.identification,tc.title as column_title');


        return $this;
    }
    
    
    /**
     * @title 报表
     * @param type $day
     * @return type
     */
    public function chart($day){
        
        
        $lists = $this->field('a.*')->select();
 
        
        //基数是多少
        $member_count_last_month_base = db('thread')->where('create_time', '<', strtotime($_GET['timea']))->count();

        
        $result = [];
        $addup = 0;
        
        for ($index = 0; $index < $day; $index++) {

            $date = date('Y-m-d', strtotime($_GET['timea']) + ($index * 86400));
            $datea = strtotime($date . ' 00:00:00');
            $dateb = strtotime($date . ' 23:59:59');
            
            $addup +=  $this->get_count($lists, $datea, $dateb);

            $result['count'][$index + 1] = $addup + $member_count_last_month_base;

            $date_short = date('d', strtotime($date));

            $result['date'][$index + 1] = "'{$date_short}'";
        }      

        return $result;
        
    }
    
    /**
     * @title 数量
     * @param type $listss
     * @param type $datea
     * @param type $dateb
     * @param type $base_count
     * @return type
     */
    private function get_count($listss, $datea, $dateb) {
        $vars = 0;
        foreach ($listss as $var) {
            $create_time = strtotime($var['create_time']);
            if ($create_time >= $datea && $create_time <= $dateb)
                $vars ++;
        }
        return $vars;
    }

}
